package com.cqc.jdk8.Jutil.function;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Function;

/**
 * java项目添加测试jar包：
 * .m2\repository\junit\junit\4.12\junit.jar
 * .m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar
 * https://blog.csdn.net/weixin_55932015/article/details/115138516
 */
public class FunctionalTest {


    @Test
    public void f5() {
        Comparator<String> comparator = (a, b) -> a.compareTo(b);
        int c1 = comparator.compare("a", "b");
        int c2 = comparator.compare("b", "b");
        int c3 = comparator.compare("c", "b");
        System.out.println(c1 + "," + c2 + "," + c3);
    }


    /**
     * Function  f1.andThen(f2).apply(xx)  先执行f1再执行f2
     */
    @Test
    public void f2() {
//        Function<String,Integer> f=(str)->Integer.valueOf(str);
        Function<String, Integer> f1 = Integer::valueOf;
        Integer r1 = f1.apply("100");
        System.out.println(r1);
        Function<String, String> f2 = f1.andThen((i) -> String.valueOf("f2" + i));
//        Function<String, String> f2 = f.andThen(String::valueOf);
        String r2 = f2.apply("100");
        System.out.println(r2);
    }

    @Test
    public void f2_1() {
        // 定义一个字符串
        String s1 = "123";
        // 使用Lambda表达式
//        this.change(s1, str -> Integer.parseInt(str) + 10, i -> i + "");
        //或者
        Function<String, Integer> f1 = str -> Integer.parseInt(str) + 10;
        Function<Integer, String> f2 = i -> "##" + i;
        String apply = f1.andThen(f2).apply(s1);
        System.out.println(apply + ",s1=" + s1);
    }

    public void change(String s, Function<String, Integer> fun1, Function<Integer, String> fun2) {
        String ss = fun1.andThen(fun2).apply(s);
        System.out.println(ss);
    }


}
